#!/bin/bash
source /root/sql.sh
menu(){
array=("数据库备份" "系统初始化" "配置静态网络" "杀死httpd进程" "退出")
for i in ${!array[@]}
do
   echo "选项$(($i+1))--${array[$i]}"
done
}
message() {
echo "请输入选项1-${#array[@]}"
}
start_os() {
echo "开始进行初始化"
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null && echo "换源完毕" || echo "换源失败"
systemctl disable --now firewalld &> /dev/null && echo "关闭防火墙成功" || echo "关闭防火墙失败"
sed -i 's/enforing/disabled/g' /etc/selinux/config && echo "Selinux已永久关闭"   || "Selinux永久关闭失败"
yum -y install vim wget lsof lrzsz net-tools &> /dev/null && echo "常用软件安装完毕" || echo "常用软件安装失败" 
echo "初始化结束"
}
kill_apache() {
process_number=`ps -ef | grep "httpd" | grep -v "grep" | awk '{print $2}'`
if [ ${#process_number} -eq 0 ];then   
   echo "apache程序未运行"
else 
   for i in ${process_number} 
   do  
      kill -9  $i 
   done
   [ $? -eq 0 ]  && echo "apache进程成功杀死" || echo ""apache进程未成功杀死
fi
}
set_net() {
read -p "输入你的网卡名称(默认为ens33):" net_name
net_name=${net_name:-ens33}
if [ -e /etc/sysconfig/network-scripts/ifcfg-$net_name  ];then
   echo "网卡${net_name}存在"
   sed -i "/^BOOT/c BOOTPROTO=none" /etc/sysconfig/network-scripts/ifcfg-$net_name && echo  "网卡${net_name}已经成功配置为手动模式" || echo "网卡${net_name}配置为手动模式失败" 
   read -p "输入你要修改的ip地址:" address
   sed -i "/^IPADDR/c IPADDR=$net_name" /etc/sysconfig/network-scripts/ifcfg-$net_name && echo  "ip地址${address}修改成功" || echo "ip地址${address}修改失败" 
   read -p "输入你要使用的掩码(默认为24位):" mask 
   mask=${mask:-24}
   sed -i "/^PREFIX/c PREFIX=$mask" /etc/sysconfig/network-scripts/ifcfg-$net_name && echo  "子网掩码成功修改为${mask}位" || echo "子网掩码未成功修改为${mask}位" 
   read -p "输入你要设置的网关:" gateway
   sed -i "/^GATEWAY/c GATEWAY=$gateway" /etc/sysconfig/network-scripts/ifcfg-$net_name && echo  "网关成功修改为$gateway" || echo "网关未成功修改为$gateway" 
   read -p "输入你要使用的dns服务器的数量" number
   if [[ $number =~  ^[0-9]+$ ]];then
      if [ $number -eq 1 ];then
          read -p "输入你的dns的ip地址" dns_ipaddr
          sed -i "/^DNS/c DNS=$dns_ipaddr" /etc/sysconfig/network-scripts/ifcfg-$net_name && echo  "dns成功修改为$dns_ipaddr" || echo "dns未成功修改为$dns_ipaddr" 
      else
        for ((i=1; i<=$number; i++))
        do 
            read -p "输入你的dns${i}的ip地址" dns_ipaddr
            sed -i "/^DNS${i}/c DNS${i}=$dns_ipaddr" /etc/sysconfig/network-scripts/ifcfg-$net_name && echo  "dns${i}成功修改为$dns_ipaddr" || echo "dns${i}未成功修改为$dns_ipaddr" 
        done
      fi
   else 
      echo "输入不合法"
   fi
else 
   echo "网卡${net_name}不存在"
fi
}
main() {
while true;
do
  menu
  read -p "输入你要使用的功能:" choice
  choice=${choice:-${#array[@]}}
  case $choice in 
  1)
    sql_sh
    ;;
  2)
    start_os
    ;;
  3) 
    set_net
    ;;
  4)
    kill_apache
    ;;
  5) 
    break
    ;;
  *)
    message
  esac
done
}
main
